'use strict';
// const Controller = require('egg').Controller;
const BaseController = require('./base');
class RoomController extends BaseController {
  // 查询所有的房间信息
  async index() {
    try {
      const { service } = this;
      const roomInfo = await service.room.getRoomList();
      console.log(roomInfo);
      this.success(roomInfo, '查询成功');
    } catch (error) {
      return error;
    }
  }
  // 新建房间信息
  async create() {
    try {
      const { ctx, service } = this;
      const file = ctx.request.files[0];
      const data = this.ctx.request.body;
      // console.log(data);
      if (file) {
        console.log('进入file');
        const result = await service.uploadQiniu.upload(file);

        data.room_pic = `http://reservation.mamenglei.top/${result.key}`;
      }

      const addRoom = await service.room.addRoom(data);
      this.success(addRoom, '新增房间信息成功');
    } catch (error) {
      this.ctx.body = {
        code: 500,
        msg: '新增房间失败',
      };
      throw error;
    }
  }
  // 房间编号查询其对应的房间信息
  async show() {
    try {
      const { ctx, service } = this;
      const id = ctx.params.id;
      const resultInfo = await service.room.getRoomById(id);
      console.log(resultInfo);
      if (resultInfo) {
        ctx.body = {
          code: 200,
          msg: 'ok',
          data: resultInfo,
        };
      } else {
        ctx.body = {
          code: 500,
          msg: '数据库未找到你要查询的信息',
        };
      }
    } catch (error) {
      return error;
    }
  }
  // 房间编号修改房间信息
  async update() {
    const { ctx, service } = this;
    const RoomId = ctx.params.id;
    const data = ctx.request.body;
    console.log(RoomId);
    console.log(data);
    const updateRes = await service.room.updateRoomInfo(RoomId, data);
    // console.log(updateRes);
    this.success(updateRes, '数据修改成功');
  }
  // 通过房间编号删除房间信息
  async destroy() {
    const { ctx, service } = this;
    const roomId = ctx.params.id;
    const delResult = await service.room.delRoomInfo(roomId);
    if (delResult) {
      this.success('ok', '房间删除成功');
    } else {
      this.error('false', '房间删除失败', 400);
    }
  }
  // 按照房间价格升序排序
  async sortPrice() {
    try {
      const { service } = this;
      const result = await service.room.sortRoomPrice();
      if (result) {
        this.success(result, '价格排序成功');
      } else {
        this.error('排序失败');
      }
    } catch (error) {
      this.error(error);
    }
  }
  // 按照房间价格排序
  async sortPriceByDesc() {
    try {
      const { service } = this;
      const result = await service.room.sortRoomPriceByDesc();
      if (result) {
        this.success(result, '价格排序成功');
      } else {
        this.error('排序失败');
      }
    } catch (error) {
      this.error(error);
    }
  }
  // 通过酒店id查询其所有房间信息
  async getRoomByHotelId() {
    try {
      const { ctx, service } = this;
      const hotelId = ctx.params.id;
      // console.log(hotelId);
      const resultInfo = await service.room.getRoomHotelById(hotelId);
      // console.log(resultInfo);
      if (resultInfo) {
        ctx.body = {
          code: 200,
          msg: 'ok',
          data: resultInfo,
        };
      } else {
        ctx.body = {
          code: 500,
          msg: '数据库未找到你要查询的信息',
        };
      }
    } catch (error) {
      return error;
    }
  }
}
module.exports = RoomController;
